<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ip extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function index(){
        $source = trim($this->input->get('s', TRUE));
        $port = trim($this->input->get('p', TRUE));
        if($source === FALSE) {
            $source = 0;
        } else {
            $source = intval($source);
        }

        if($port === FALSE) {
            $port = $this->get_port($source);
        } else {
            $port = intval($port);
        }

        $ip = trim($this->input->get('ip', TRUE));
        if(!$ip){
        	if (getenv("HTTP_CLIENT_IP")){
        		$ip = getenv("HTTP_CLIENT_IP");
        	} else if(getenv("HTTP_X_FORWARDED_FOR")){
        		$ip = getenv("HTTP_X_FORWARDED_FOR");
        	} else if(getenv("REMOTE_ADDR")){
        		$ip = getenv("REMOTE_ADDR");
        	} else {
        		$ip = NULL;
        	}
        }
        
        if(!is_null($ip)) {
            $this->load->model('universal/mobility_ip_model');
            if($port < 1) {
                $port = 80;
            }
            $this->mobility_ip_model->label_mobility_ip_by_source($source);
            $this->mobility_ip_model->create_mobility_ip($ip , $source, $port);
            
        }
	}

	function jingdong($ip , $source){
		
		$source_arr = array(1,2,3,4,5,6,10,11,12,15,16,17,20,27,31,32,33,34,35,36,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75);
		
		if(!in_array($source, $source_arr)){
			return false;
		}
		
		$get_url = "http://211.151.8.205/com.jd.jrdp.monitor.proxy.monitor.service.IThirdPartUpdateIPService/JSF_0.0.1/updateAdslIp";
		
		$arr = array();
		
		$arr[] = "h2data." . $source;
		$arr[] = $ip . ":10008";
		if($source > 50){
			$arr[] = time() + 300;
		}else{
			$arr[] = time() + 120;
		}
		$arr[] = "3rdParty.h2data";
		
		$post_str = json_encode($arr);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $get_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'token: 98C2E6A1396C0AE6'
		));
		
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
		$data = curl_exec($ch);
		curl_close($ch);
		echo $data;
	}
	
    public function verify(){
        echo "yes";
    }

    public function usability() {
        echo json_encode(array('result' => TRUE));
    }

    public function get_port($source) {
        switch ($source){
            case 0:
                return 20068;
                break;
            case 1:
                return 20316;
                break;
            case 2:
                return 20116;
                break;
            case 3:
                return 20336;
                break;
            default:
                return 0;
        }
    }

///////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	* 检查是否有 ip 更新延时的 ip
	*
	* @access public
	* @return string $mixed
	*/
	public function check_delayed() {
		$this->load->model('universal/mobility_ip_model');
		$this->load->model('universal/mobility_ip_delayed_model');
		if(false == ($delayed_ip_list = $this->mobility_ip_model->get_delayed_list())){
			return false;
		}


		foreach ($delayed_ip_list as $ip) {
			$this->mobility_ip_delayed_model->log_delayed_ip($ip);
		}
	}
	
	public function check_delayed_crontab() {
		$this->load->model('universal/mobility_ip_model');
		$this->load->model('universal/mobility_ip_delayed_model');
		if(false == ($delayed_ip_list = $this->mobility_ip_model->get_delayed_list())){
			return false;
		}
		
		$msg = "";
		foreach ($delayed_ip_list as $delayed_ip){
			if($delayed_ip['source'] < 40){
				continue;
			}
			$msg = $msg . "ADSL服务器 [".$delayed_ip['source']."] 延迟拨号超过".(time() - $delayed_ip['ctime'])."秒\n";
		}
		
		if($msg){
			$msg .= "请前往 http://59.110.86.68:802/ip/status 查看详情";
			
			$url = "http://101.200.126.90:8080/api/api/api";
			$post = array();
			
			$post['send_type'] = 2;
			$post['recive'] = "ADSL";
			$post['msg'] = $msg;
			
			$this->load->helper('my_http_helper');
			byCurlRequest($url, $post);
		}
		
	}

	/**
	* 查看 adsl服务器 ip更新状况
	*
	* @access public
    * @param array $time_rang_type 要查看的时间范围类别 today 当天  week 本周 month 本月 [Optional]
	* @return string $mixed
	*/
	public function status($time_rang_type ='day') {
		$this->load->model('universal/mobility_ip_model');
		$this->load->model('universal/mobility_ip_error_model');
		$this->load->model('universal/mobility_ip_delayed_model');

		//ip状态列表
		$ip_list_status = $this->mobility_ip_model->get_ip_status();
		//var_dump($ip_list_status);

		//adsl服务器 在指定时间范围内 变更的ip总数
		$adsl_ip_count = $this->mobility_ip_model->get_adsl_ipcount($time_rang_type);
		//var_dump($adsl_ip_count);

		foreach ($ip_list_status as $key => $source) {
			//编号为 source的机器 在指定时间范围内 变更的ip总数
			$ip_count = $this->get_ip_count($source['source'],$adsl_ip_count);

			//adls服务器ip散列度
			$cardinality_list = $this->mobility_ip_model->get_ip_cardinality($source['source'],$time_rang_type);
			$cardinality = count($cardinality_list).'/'.$ip_count;

			$ip_list_status[$key]['cardinality'] = $cardinality;
		}
		//var_dump($ip_list_status);

		//错误情况列表
		$template_data['error_list'] = $this->mobility_ip_error_model->get_error_count($time_rang_type);

		//更新IP延时列表
		$template_data['delayed_list'] = $this->mobility_ip_delayed_model->get_delayed_count($time_rang_type);

		$template_data['ip_list_status'] = $ip_list_status;
		$this->load->view('ip_status', $template_data);
	}

	/**
	* 编号为 source的机器 在指定时间范围内 变更的ip总数
	*
	* @access public
	* @param int  $source adsl服务器的source编号 [Must]
	* @param array $adsl_ip_count adsl服务器 在指定时间范围内 变更的ip总数 [Must]
	* @return int $mixed
	*/
	public function get_ip_count($source,$adsl_ip_count) {
		if(empty($source) || empty($adsl_ip_count)) return false;

		foreach ($adsl_ip_count as $adsl_source) {
				if($adsl_source['source'] == $source) return $adsl_source['c'];
		}

		return false;
	}
}